import Vue from 'vue'
import store from '@/store'
import router from '@/router'
import { getStorage } from '@/utils'

function apiStatusHandler(level, message, liveTime = 1.5) {
  let status = ''
  switch (level) {
    case 0:
    case 200:
      this.$message.success(message, liveTime)
      status = 'success'
      break
    case 99:
      this.$message.error(message, liveTime)
      status = 'error'

      break
    default:
      this.$message.warn(message, liveTime)
      status = 'warning'
      break
  }
  return status
}

function variablesType(variable) {
  return Object.prototype.toString
    .call(variable)
    .replace(/^\[object (\S+)\)]$/, '$1')
}

function isArray(variable) {
  return variablesType(variable) === '[object Array]'
}

function isObj(variable) {
  return variablesType(variable) === '[object Object]'
}

function isFunc(variable) {
  return variablesType(variable) === '[object Function]'
}

function isEmptyObj(obj) {
  const len = Object.keys(obj)
  return len.length === 0
}

// 首次进来自动登录（游客
// async function firstLogin() {
//   let token = getStorage('token', 0)
//   if (!token || token === '') {
//     let form = {
//       email: 'visitor@qq.com',
//       password: '123456'
//     }
//     const { code } = await store.dispatch('user/LoginForm', form)
//     const { initRouter, truthRouter } = Vue.prototype.$userRouterInfo
//     if (code === 200) {
//       router.push(truthRouter || initRouter)
//     }
//     // store.dispatch('user/LoginForm',form).then((res)=>{
//     //   if(res.code === 200){
//     //     // window.location.reload()   /operation/issue?key=operationIssue
//     //     this.$router.push('/platformPortal')
//     //   }
//     // })
//   }
// }
// 自动获取数据中心的token
// async function getDatabaseBinToken() {
//   let database_token = getStorage('database_token', 0)
//   if (database_token) return
//   store.dispatch('user/getDatabaseBinToken')
// }

window.APP_CONFIG = {
  CRYPTOJSKEY: '88Uv*RT@ReLhg~PV',
  EXPRETYPE: 1,
  baiDuTongJi: false,
  loginData: {
    client_id: 'client_2',
    client_secret: '123456'
  }
}

// 用户的路由信息
Vue.prototype.$userRouterInfo = {
  initRouter: '/platformPortal',
  truthRouter: ''
}
Vue.prototype.$variablesType = variablesType
Vue.prototype.$isArrayType = isArray
Vue.prototype.$isObj = isObj
Vue.prototype.$isFunc = isFunc
Vue.prototype.$isEmptyObj = isEmptyObj
Vue.prototype.$apiStatusHandler = apiStatusHandler

// firstLogin()
// getDatabaseBinToken()
